package com.maddox.relax.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.ViewFlipper;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Maddox
 * 2021-11-23
 * <p>
 * 切换布局,可自定义布局样式,动画效果,间隔时间
 * 数据源
 * List<Integer> ids = new ArrayList<>();
 * ids.add(R.mipmap.liqin7);
 * ids.add(R.mipmap.picture1);
 * ids.add(R.mipmap.picture2);
 * 当前控件
 * GiveCnView cnView = findViewById(R.id.flipper);
 * 设置适配器
 * cnView.setAdapter(new GiveCnView.FineAdapter<Integer>(this, R.layout.item_view_fl, ids) {
 * @Override public void bindView(View itemView, Integer integer) {
 * 回调出每个滚动的子视图以及对应位置的数据,自己去设置相应的数据
 * ImageView imageView = (ImageView) itemView;
 * imageView.setImageResource(integer);
 * }
 * });
 */
public class AutoFlipperView extends ViewFlipper {

    private Context mContext;
    private int animationDuration = 2000;
    private FineAdapter fineAdapter;

    public AutoFlipperView(Context context) {
        this(context, null);
    }

    public AutoFlipperView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();

    }

    private void init() {
        initAnimation();
    }

    /**
     * 必设置  必设置 必设置
     * 设置自定义布局以及展示的适配器
     *
     * @param adapter 适配器
     * @param <T>     数据类型
     */
    public <T> void setAdapter(FineAdapter<T> adapter) {
        if (adapter == null) {
            return;
        }
        fineAdapter = adapter;
        List<View> adapterViews = adapter.getViews();
        for (View itemView : adapterViews) {
            addView(itemView);
        }
        startFlipping();
    }

    /**
     * 点击事件的监听
     *
     * @param listener 监听器
     * @param <T>      回调出的数据实体
     */
    public <T> void setOnItemClickListener(OnItemClicked<T> listener) {
        setOnClickListener(v -> {
            int index = getDisplayedChild();
            T itemData = (T) fineAdapter.getItemData(index);
            if (listener != null)
                listener.clickData(itemData);
        });
    }


    //默认滚动动画效果
    private void initAnimation() {
        TranslateAnimation inAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0f);
        TranslateAnimation outAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, -1.0f);
        inAnimation.setDuration(animationDuration);
        outAnimation.setDuration(animationDuration);
        setInAnimation(inAnimation);
        setOutAnimation(outAnimation);
    }

    /**
     * 设置自定义动画效果
     *
     * @param inResId  滚入动画
     * @param outResId 滚出动画
     */
    public void setAnimation(int inResId, int outResId) {
        Animation inAnimation = AnimationUtils.loadAnimation(mContext, inResId);
        Animation outAnimation = AnimationUtils.loadAnimation(mContext, outResId);
        setInAnimation(inAnimation);
        setOutAnimation(outAnimation);
    }


    /**
     * 设置动画切换时长
     *
     * @param animationDuration 毫秒数
     */
    public void setAnimationDuration(int animationDuration) {
        this.animationDuration = animationDuration;
    }

    private int dp2px(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, mContext.getResources().getDisplayMetrics());
    }

    public interface OnItemClicked<T> {
        void clickData(T data);
    }

    static abstract class FineAdapter<T> {
        private Context context;
        private List<T> datas;
        private int layoutId;

        public FineAdapter(Context context, int layoutId, List<T> datas) {
            this.context = context;
            this.layoutId = layoutId;
            this.datas = datas;
            if (this.datas == null) {
                this.datas = new ArrayList<>();
            }
        }

        public List<View> getViews() {
            LayoutInflater inflater = LayoutInflater.from(context);
            List<View> viewList = new ArrayList<>();
            for (int i = 0; i < datas.size(); i++) {
                View itemView = inflater.inflate(layoutId, null);
                bindView(itemView, datas.get(i));
                viewList.add(itemView);
            }
            return viewList;
        }

        public T getItemData(int position) {
            return datas.get(position);
        }

        public abstract void bindView(View itemView, T t);
    }


}
